package cn.appsys.controller.backend;

import cn.appsys.pojo.AppCategory;
import cn.appsys.pojo.BackendUser;
import cn.appsys.pojo.DataDictionary;
import cn.appsys.service.developer.DataDictionaryService;
import cn.appsys.tools.Constants;
import cn.appsys.tools.PageSupport;
import cn.appsys.tools.ReturnObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manager/backend/dictionary")
public class DataDictionaryController {

    @Autowired
    private DataDictionaryService dataDictionaryService;
    @RequestMapping("/toDataDicetionary")
    public String toDataDicetionary(String pageIndex, Model model,String typeName,String valueName){
        //这是pagesize
        int pageSize = Constants.pageSize;
        //设置当前页码
        Integer currentPageNo = 1;
        if (pageIndex != null) {
            try {
                currentPageNo = Integer.valueOf(pageIndex);
            } catch (NumberFormatException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
        //封装参数
        Map<String, Object> map = new HashMap<>();
        map.put("beginNo", (currentPageNo - 1) * pageSize);
        map.put("pageSize", pageSize);
        map.put("typeName", typeName);
        map.put("valueName", valueName);
        //总记录数
        int totalCount = 0;
        try {
            totalCount = dataDictionaryService.queryDataDictionaryCount(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //总页数
        PageSupport pages = new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
        //校验页码
        if (currentPageNo < 1) {
            currentPageNo = 1;
        } else if (currentPageNo > totalPageCount) {
            currentPageNo = totalPageCount;
        }
        List<DataDictionary> dataDictionaryList = null;
        try {
            dataDictionaryList = dataDictionaryService.queryDataDictionary(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<DataDictionary> dataDictionaryTypeNameList = dataDictionaryService.queryDataDictionaryTypeNameForDistinct();
        List<DataDictionary> dataDictionaryByTypeNameList = null;
        if(typeName!=null){
            dataDictionaryByTypeNameList = getDataDictionaryByTypeNameList(typeName);
        }
        model.addAttribute("dataDictionaryList", dataDictionaryList);
        model.addAttribute("dataDictionaryTypeNameList", dataDictionaryTypeNameList);
        model.addAttribute("dataDictionaryByTypeNameList", dataDictionaryByTypeNameList);
        model.addAttribute("pages", pages);
        model.addAttribute("typeName", typeName);
        model.addAttribute("valueName", valueName);
        return "backend/datadictionary";
    }

    public List<DataDictionary> getDataDictionaryByTypeNameList (String typeName){
        List<DataDictionary> dataDictionaryList = null;
        try {
            dataDictionaryList = dataDictionaryService.queryDataDictionaryByTypeName(typeName);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return dataDictionaryList;
    }

    @RequestMapping("/getValueNameList.json")
    @ResponseBody
    public Object getValueNameList(String typeName){
        return getDataDictionaryByTypeNameList(typeName);
    }

    @RequestMapping("/toAddDataDictionary")
    public String toAddDataDictionary(){
        return "backend/adddatadictionary";
    }

    @RequestMapping("/addDataDictionary")
    @ResponseBody
    public Object addDataDictionary(DataDictionary dataDictionary, HttpSession session){
        BackendUser backendUser = (BackendUser) session.getAttribute(Constants.USER_SESSION);
        ReturnObject returnObject = new ReturnObject();
        Date date = new Date();
        //封装参数
        dataDictionary.setCreatedBy(backendUser.getId());
        System.out.println(new Date());
        System.out.println(date);
        dataDictionary.setCreationDate(date);
        try {
            int ret = dataDictionaryService.saveCreateDataDictionary(dataDictionary);
            if(ret>0){
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
            }else {
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("添加失败，请稍后再试!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("添加失败，请稍后再试!");
        }
        return returnObject;

    }

    @RequestMapping("/deleteDataDictionaryById")
    @ResponseBody
    public Object deleteDataDictionaryById(int id){
        ReturnObject returnObject = new ReturnObject();
        try {
            int ret = dataDictionaryService.deleteDataDictionaryById(id);
            if(ret>0){
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
            }else {
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("删除失败，请稍后重试!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("删除失败，请稍后重试!");
        }
        return returnObject;
    }

    @RequestMapping("/toEditDataDictionary")
    public String toEditDataDictionary(int id, Model model){
        DataDictionary dataDictionary = null;
        try {
            dataDictionary = dataDictionaryService.queryDataDictionaryById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("dataDictionary",dataDictionary);
        return "backend/editdatadictionary";
    }

    @RequestMapping("/editDataDictionary")
    @ResponseBody
    public Object editDataDictionary(DataDictionary dataDictionary,HttpSession session){
        ReturnObject returnObject = new ReturnObject();
        BackendUser loginBackendUser = (BackendUser) session.getAttribute(Constants.USER_SESSION);
        Date date = new Date();
        //封装参数
        dataDictionary.setModifyBy(loginBackendUser.getId());
        dataDictionary.setModifyDate(date);
        try {
            int ret = dataDictionaryService.editDataDictionarById(dataDictionary);
            if(ret>0){
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
            }else {
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("修改失败，请稍后再试!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("修改失败，请稍后再试!");
        }
        return returnObject;
    }
}
